package com.ruoyi.bussine.utils;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.bussine.domain.entity.TbArea;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author zhengyz
 * @date 2025/3/17
 */
@Component
public class AreaUtils {

    // 注入静态的redisCache
    private static RedisCache redisCache;

    @Autowired
    public AreaUtils(RedisCache redisCache) {
        AreaUtils.redisCache = redisCache;
    }

    /**
     * 批量插入地址信息
     *
     * @param tbAreaList
     */
    public static void setAreaCacheList(List<TbArea> tbAreaList) {
        Map<String, TbArea> map = new HashMap<>();
        Map<String, Set<TbArea>> parentMap = new HashMap<>();
        for (TbArea tbArea : tbAreaList) {
            map.put(tbArea.getAreaCode(), tbArea);
            Set<TbArea> children = new HashSet<>();
            if (parentMap.containsKey(tbArea.getParentId().toString())) {
                children = parentMap.get(tbArea.getParentId().toString());
            }
            children.add(tbArea);
            parentMap.put(tbArea.getParentId().toString(), children);
        }
        redisCache.setCacheMap(CacheConstants.REDIS_AREA_KEY, map);
        // 循环parentMap
        for (Map.Entry<String, Set<TbArea>> entry : parentMap.entrySet()) {
            redisCache.setCacheSet("areaParent:" + entry.getKey(), entry.getValue());
        }
    }

    public static Set<TbArea> getListByParent(String parentId) {
        if (!"1".equals(parentId)) {
            TbArea area = getByCode(parentId);
            // 现在地址信息只到区域
            if (area == null || 3 == area.getLevel()) {
                return null;
            }
        }
        String key = "areaParent:" + parentId;
        // 不存在这个键，说明没有加载过
        if (!redisCache.hasKey(key)) {
            Map<String, Set<TbArea>> parentMap = new HashMap<>();
            Map<String, TbArea> areaMap = redisCache.getCacheMap(CacheConstants.REDIS_AREA_KEY);
            for (Map.Entry<String, TbArea> entry : areaMap.entrySet()) {
                Set<TbArea> children = new HashSet<>();
                if (parentMap.containsKey(entry.getValue().getParentId().toString())) {
                    children = parentMap.get(entry.getValue().getParentId().toString());
                }
                children.add(entry.getValue());
                parentMap.put(entry.getValue().getParentId().toString(), children);
            }
            // 循环parentMap
            for (Map.Entry<String, Set<TbArea>> entry : parentMap.entrySet()) {
                redisCache.setCacheSet("areaParent:" + entry.getKey(), entry.getValue());
            }
        }
        return redisCache.getCacheSet(key);
    }

    /**
     * 单个插入地址信息
     *
     * @param tbArea
     */
    public static void setAreaCache(TbArea tbArea) {
        Map<String, TbArea> map = new HashMap<>();
        map.put(tbArea.getAreaCode(), tbArea);
        redisCache.setCacheMap(CacheConstants.REDIS_AREA_KEY, map);
    }

    /**
     * 获取所有地址信息
     *
     * @return
     */
    public static List<TbArea> getAll() {
        Map<String, TbArea> areaMap = redisCache.getCacheMap(CacheConstants.REDIS_AREA_KEY);
        if (ObjectUtil.isNull(areaMap)) {
            return null;
        }
        List<TbArea> areaList = new ArrayList<>();
        for (Map.Entry<String, TbArea> entry : areaMap.entrySet()) {
            areaList.add(entry.getValue());
        }
        return areaList;
    }

    /**
     * 根据code 获取地址信息
     *
     * @param areaCode
     * @return
     */
    public static TbArea getByCode(String areaCode) {
        if (StringUtils.isBlank(areaCode)) {
            return null;
        }
        return redisCache.getCacheMapValue(CacheConstants.REDIS_AREA_KEY, areaCode);
    }

    /**
     * 根据code 获取地址信息，没查询到则返回默认值
     *
     * @param areaCode 编码
     * @param defVal   返回的默认值
     * @return
     */
    public static TbArea getByCodeWithDefVal(String areaCode, TbArea defVal) {
        TbArea tbArea = getByCode(areaCode);
        return ObjectUtil.isNotEmpty(tbArea) ? tbArea : defVal;
    }

    /**
     * 根据code 获取地址名称
     *
     * @param areaCode
     * @return
     */
    public static String getNameByCode(String areaCode) {
        TbArea tbArea = getByCode(areaCode);
        return ObjectUtil.isNotNull(tbArea) ? tbArea.getAreaName() : "";
    }

    /**
     * 根据code 获取地址名称
     *
     * @param areaCode 编码
     * @param defVal   返回的默认值
     * @return
     */
    public static String getNameByCodeWithDefVal(String areaCode, String defVal) {
        TbArea tbArea = getByCode(areaCode);
        return ObjectUtil.isNotNull(tbArea) ? tbArea.getAreaName() : defVal;
    }

    /**
     * 根据code删除地址信息
     */
    public static void deleteByCode(String areaCode) {
        if (StringUtils.isBlank(areaCode)) {
            return;
        }
        redisCache.deleteCacheMapValue(CacheConstants.REDIS_AREA_KEY, areaCode);
        redisCache.deleteObject("areaParent:" + areaCode);
    }

    /**
     * 删除所有地址缓存
     */
    public static void deleteAll() {
        redisCache.deleteObject(CacheConstants.REDIS_AREA_KEY);
    }

}
